package com.zegome.game.gomestory.main.scene;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import org.json.JSONArray;
import org.json.JSONObject;

import android.inputmethodservice.Keyboard.Key;
import android.util.Base64;
import android.util.Log;
import android.view.KeyEvent;
import aurelienribon.tweenengine.BaseTween;
import aurelienribon.tweenengine.Tween;
import aurelienribon.tweenengine.TweenCallback;
import aurelienribon.tweenengine.TweenManager;
import aurelienribon.tweenengine.equations.Linear;
import aurelienribon.tweenengine.equations.Sine;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener.ChangeEvent;
import com.zegome.facebook.IFacebookBridge;
import com.zegome.facebook.IFacebookProcessorListener;
import com.zegome.game.entity.sprite.Sprite;
import com.zegome.game.gomestory.MainActivity;
import com.zegome.game.gomestory.main.A;
import com.zegome.game.gomestory.main.Center;
import com.zegome.game.gomestory.main.D;
import com.zegome.game.gomestory.main.helper.BackListener;
import com.zegome.game.gomestory.main.helper.DailyGift;
import com.zegome.game.gomestory.main.helper.GameAdvertise;
import com.zegome.game.gomestory.main.helper.Gift;
import com.zegome.game.gomestory.main.helper.GiftRequest;
import com.zegome.game.gomestory.main.helper.IMAction;
import com.zegome.game.gomestory.main.helper.IMActionBoolean;
import com.zegome.game.gomestory.main.helper.IWMAction;
import com.zegome.game.gomestory.main.helper.LazyLoadImage;
import com.zegome.game.main.AndroidGameController;
import com.zegome.game.screen.GameScreen;
import com.zegome.game.util.helper.WidgetAccessor;
import com.zegome.payment.appota.IAppotaPaymentBridge;

public class BaseGameScreen extends GameScreen implements InputProcessor , IFacebookProcessorListener{
	
	public Center ctr;
	
	protected Stage stage;
	protected TweenManager tweenManager;
	
	private boolean isHasButtonBar;
	private boolean isFadeInWhenShow = true;
	private boolean isHasNotify = true;
	private boolean isNetWorkInBackground;	
	
	private Image black;	
		
	public int state;
		
	private Sprite iconGomeSmall;
	private Image iconGomeSmallStage;
		
	@SuppressWarnings("unused")
	private IMAction load, loaded, end;
	
	private IMActionBoolean waitAction;
	
	public IMAction afterAction;
	public int mAction;
	public IFacebookBridge mFBBridge;
	
	public BaseGameScreen(AndroidGameController controller,String name,final IFacebookBridge bridge) {
		super(controller, name);
		init();
		this.mFBBridge = bridge;
	}
	
	public void addIconGomeSmallStage(){
		if(iconGomeSmallStage==null){
			iconGomeSmallStage = ctr.stageHelper.createImage(ctr.a.iconSmallGome, 666, 410);
			stage.addActor(iconGomeSmallStage);
		}
	}
		
	public void addIconGomeSmall(){		
		if(iconGomeSmall!=null){
			this.removeChild(iconGomeSmall);
		}
		
		if(iconGomeSmall==null){
			iconGomeSmall = new Sprite(666, 410, ctr.a.iconSmallGome);
		}
		
		attachChild(iconGomeSmall);		
	}
	
	public void setCtr(Center ctr){
		this.ctr = ctr;
	}
	
	public BaseGameScreen(AndroidGameController controller, String name, SpriteBatch spriteBatch) {
		super(controller, name, spriteBatch);
		init();
	}
	
	public void init()
	{
		stage = mController.getStage();
		tweenManager = mController.getTween();
		setConfig(true, true,true,true);
	}

	public void setConfig(boolean isFadeInWhenShow, boolean isHasNotify, boolean isHasButtonBar, boolean isNetWorkInBackground)
	{
		this.isFadeInWhenShow = isFadeInWhenShow;
		this.isHasNotify = isHasNotify;
		this.isHasButtonBar = isHasButtonBar;
		this.isNetWorkInBackground = isNetWorkInBackground;
	}
		
	public void fadeIn()
	{		
		black.remove();
		Color color = black.getColor();
		color.set(0.0f, 0.0f, 0.0f, 0.80f);
		color.a = 0.90f;
		black.setColor(color);		
		stage.addActor(black);
		black.setVisible(true);
		ctr.a.isFadeScreen = true;
		Tween.to(black, WidgetAccessor.OPACITY, 0.6f).target(0.0f).ease(Linear.INOUT)
		.setCallback(new TweenCallback() {			
			@SuppressWarnings("synthetic-access")
			@Override
			public void onEvent(int type, BaseTween<?> source) {
				black.remove();
				black.setVisible(false);
				ctr.a.isFadeScreen = false;
			}
		})
		.start(tweenManager);
	}
	
	public void fadeOut(final TweenCallback callback)
	{				
		ctr.a.isFadeScreen = true;
		black.remove();
		Color color = black.getColor();
		color.set(0.0f, 0.0f, 0.0f, 0.0f);
		color.a = 0.0f;
		black.setColor(color);
		stage.addActor(black);
		black.setVisible(true);
		
		Tween.to(black, WidgetAccessor.OPACITY, 0.6f).target(0.90f).ease(Linear.INOUT)
		.setCallback(new TweenCallback() {
			
			@Override
			public void onEvent(final int arg0, final BaseTween<?> arg1) {
				// TODO Auto-generated method stub
				postRunnable(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						ctr.a.isFadeScreen = false;
						callback.onEvent(arg0, arg1);						
					}
				});				
			}
		})
		.start(tweenManager);
	}
	
	public void show()	
	{			
		
		if(isHasNotify){
			ctr.autoRunText.init();
			stage.addActor(ctr.autoRunText);
			stage.addActor(ctr.autoRunText.image);
			stage.addActor(ctr.autoRunText.button);
		}
		
		if(black==null){
			black = ctr.stageHelper.createImage(ctr.a.blackRegion, 0, 0, (800), (480));
		}
		
		if(isHasButtonBar){
			ctr.addSettingManager(this, getStage(), getTween());
		}
		
		if(isFadeInWhenShow == true)
		{
			fadeIn();
		}				
		InputMultiplexer inputMultiplexer = new InputMultiplexer();
		inputMultiplexer.addProcessor(stage);
		inputMultiplexer.addProcessor(this);
		Gdx.input.setInputProcessor(inputMultiplexer);
		Gdx.input.setCatchBackKey(true);
		
		state = 1;
		
	}
	

	
	public void setWaitAction(IMActionBoolean action){				
		this.waitAction = action;
		waitAction.begin();
	}	
	    
	public static float tickTimeLogined=0;
	public static float tickTimeRefreshAdmob=0;	
	
	public void update(float pDeltaTime) 
	{			
		if(ctr.d.isLogined){		
			if(tickTimeLogined>=900){
				ctr.d.money += 100;
				ctr.androidHelper.showToast(ctr.lang.amassDiamond);
				ctr.d.saveAccount();
				tickTimeLogined = 0;
			}
			else{
				tickTimeLogined += pDeltaTime;
			}
		}
		else{
			tickTimeLogined = 0;
		}
		
		if(MainActivity.isAdmob && ctr.d.isNetworkAvaiable){		
			if(tickTimeRefreshAdmob>=600){
				tickTimeRefreshAdmob = 0;
				ctr.androidHelper.admobControl.refresh();
			}
			else{
				tickTimeRefreshAdmob += pDeltaTime;
			}
		}
		else{
			tickTimeRefreshAdmob = 0;
		}
				
		
		
		if(ctr.d.isNetworkAvaiable && ctr.d.isLogined()){
		if(ctr.d.lazyLoad!=null){
			if(ctr.d.lazyLoad.size()!=0){
				if(ctr.d.isLoadingLazy==false){					
					for (int i = 0;i<ctr.d.lazyLoad.size();i++) {						
						if(ctr.d.lazyLoad.elementAt(i).isFinish == false){
							if(ctr.d.lazyLoad.elementAt(i).isSending == false){
								ctr.androidHelper.sendGetImage(this, ctr.d.lazyLoad.elementAt(i));								
							}
							break;
						}
					}
				}
			}
		}
		}
		
		if(ctr.d.isNetworkAvaiable){
		if(ctr.d.gameAdvertiseLoad!=null){
			if(ctr.d.gameAdvertiseLoad.size()!=0){
				if(ctr.d.isLoadingGameAdvertise==false){					
					for (int i = 0;i<ctr.d.gameAdvertiseLoad.size();i++) {						
						if(ctr.d.gameAdvertiseLoad.elementAt(i).isFinish == false){
							if(ctr.d.gameAdvertiseLoad.elementAt(i).isSending == false){
								ctr.androidHelper.sendGetAdvertise(this, ctr.d.gameAdvertiseLoad.elementAt(i));								
								//System.out.println("ĐÂY AGAVADVEG");
							}
							break;
						}
					}
				}
			}
		}
		}
		
		
		
			if(ctr.d.isNetworkAvaiable){
				
				if(ctr.d.isSendEmail == false){					
					if(ctr.d.isSendingEmail == false){
						if(ctr.d.emailPre!=null && ctr.d.emailPre.length()>5){
							ctr.d.isSendingEmail = true;						
							ctr.androidHelper.sendMail();
						}
						else{
							ctr.d.isSendEmail = true;
						}
					}
				}
				
				if(ctr.d.isSendEmail && ctr.d.isCheckVersion == false){
					if(ctr.d.isSendingCheckVersion == false){						
						ctr.d.isSendingCheckVersion = true;						
						ctr.androidHelper.sendCheckVersion();
					}
				}
			}
		
		if(ctr.d.isVersionLatest){
		if(ctr.d.isHasAccount == false){
			if(ctr.d.isNetworkAvaiable){
				if(ctr.d.isCheckNotifyAnonymous == false){
					if(ctr.d.isSendingCheckNotifyAnonymous == false){						
						ctr.d.isSendingCheckNotifyAnonymous = true;
						ctr.androidHelper.sendGetNotice();
					}
				}
			}
		}
		}
		
		if(D.isChangeAvatar == true){				            		              		            
				if(ctr.a.avatarTexture!=null)
					ctr.a.avatarTexture.dispose();
				ctr.a.avatarTexture = null;
				/* Cach khoi tao texture cu : hoat dong binh thuong */
				ctr.a.avatarTexture = new Texture(Gdx.files.external("data/avatar/"+ctr.d.name));					
				/* Cach khoi tao moi tu byte data */
				//A.avatarTexture = new Texture(new Pixmap(bytes, 0, bytes.length));
				if(ctr.a.avatarTexture!=null){
					ctr.a.avatarMe = new TextureRegion(ctr.a.avatarTexture);
				}												
				ctr.stageHelper.avatar.setDrawable(new TextureRegionDrawable(ctr.a.avatarMe));
				
				ctr.d.isNewAvatarNotSync = true;
				ctr.d.isSyncedNewAvatar = false;
				ctr.d.isSyncingNewAvatar = false;
				ctr.d.saveAvatarInfo();				
				D.isChangeAvatar = false;
		}
		
		if(waitAction!=null){
			if(waitAction.isFinish()){
				waitAction.end();
				waitAction = null;
			}
		}
		
		if(state  == 2){			
			if(getController().updateAssets()){							
				loaded.action();
				state = 3;
				if(end != null){
					end.action();
				}
			}
		}
		
		else if (state == 1){
		if(ctr.stageHelper.infoUserFrame!=null){
			ctr.stageHelper.moneyStr.setText(" "+ctr.d.money+"");
			
			//ctr.stageHelper.infoUserFrame.setVisible(ctr.d.isHasAccount);
			
			if(ctr.d.isLogined)
			{
				ctr.stageHelper.diamond.setVisible(true);
				ctr.stageHelper.nameStr.setText(" "+ctr.d.moneyOnline+"");
				ctr.stageHelper.nameStr.setX(110);
			}
			else{
				ctr.stageHelper.diamond.setVisible(false);
				if(ctr.d.name.length()>10){				
				ctr.stageHelper.nameStr.setText(ctr.d.name.substring(0, 5)+"..."+ctr.d.name.substring(ctr.d.name.length()-2, ctr.d.name.length()));
				}
				else{
					ctr.stageHelper.nameStr.setText(ctr.d.name);
				}
				ctr.stageHelper.nameStr.setX(92);
				if(ctr.d.isHasAccount==false){
					ctr.stageHelper.nameStr.setText("Noname");
				}				
			}					
		}
		
		if(ctr.d.isLogined()){
		if(ctr.mGiftRequests.size()>0){			
			if(ctr.a.isHasDialog() == false){
				String users = "";
				int total = 0;
				for (GiftRequest temp : ctr.mGiftRequests) {					
					users+=temp.getSender()+",";
					total += temp.getValue();
				}
				users = users.substring(0, users.length()-2);
				final int total1 = total;
				ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.getGiftViaFB(users, total), 1, new ChangeListener() {					
					@Override
					public void changed(ChangeEvent arg0, Actor arg1) {
						// TODO Auto-generated method stu
						ctr.d.money += total1;
						ctr.d.saveAccount();
					}
				}, null);
				ctr.mGiftRequests.removeAllElements();
			}
		}

		if(ctr.d.gifts.size()>0){
			if(ctr.a.isHasDialog() == false){
				final Gift temp = ctr.d.gifts.elementAt(0);
				ctr.d.gifts.removeElementAt(0);
				ctr.stageHelper.createDialog(stage, tweenManager, temp.user_send + " "+(temp.zegome_gold!=0?temp.zegome_gold+" gold ":temp.coupon) + temp.message, 2, new ChangeListener() {					
					@Override
					public void changed(ChangeEvent arg0, Actor arg1) {
						// TODO Auto-generated method stub
						ctr.androidHelper.sendGift(stage, tweenManager, temp.gift_id, temp.zegome_gold,temp.coupon);
					}
				}, null);
			}
		}
		
		if(ctr.d.dailyGifts.size()>0){
			if(ctr.a.isHasDialog() == false){
				final DailyGift temp = ctr.d.dailyGifts.elementAt(0);
				ctr.d.dailyGifts.removeElementAt(0);
				ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.dailyGift + " "+temp.gold +" gold ", 1, null, null);
				ctr.d.moneyOnline += temp.gold;
				ctr.d.saveAccount();
			}
		}
		}
		}
		if(MainActivity.isAdmob){
		if(ctr.a.isHasDialog() && ctr.a.isFadeScreen == false){
			if(ctr.androidHelper.admobControl.isShow()&&ctr.a.isDialogCloseStyle()){
				ctr.androidHelper.admobControl.hide();
			}			
	
		}
		else{
			if(ctr.androidHelper.admobControl.isHide()){
				ctr.androidHelper.admobControl.show();
			}	
		}			
		}
		if(isNetWorkInBackground && (ctr.d.isVersionLatest&&ctr.d.isCheckVersion)){
		
		if(ctr.d.isHasSessionId == false){
			if(ctr.d.isSendingGetSessionId == false && ctr.d.isNetworkAvaiable){	
				ctr.d.isSendingGetSessionId = true;
				ctr.androidHelper.getSessionId();
			}
		}
		
		if(ctr.d.isSendingLogin == false){
		if(ctr.d.isNetworkAvaiable && ctr.d.isHasAccount){
			if(ctr.d.isLogined==false && ctr.d.isHasSessionId){
				// Send Login + CallBack
				ctr.d.isSendingLogin = true;
				final IWMAction loginSuccess = new IWMAction() {					
					@Override
					public void action(String result) {						
						try{
						JSONObject jsObj = new JSONObject(result);
						// TODO Auto-generated method stub
						if (result == null) {
							ctr.d.isSendingLogin = false;
						} else {																				
							 if(jsObj.getString("code").equals("1")){
								 
								 ctr.d.name = ctr.d.namePre;
								 ctr.d.email = ctr.d.emailPre;
								 ctr.d.pass = ctr.d.passPre;	
								 
								 JSONObject content = jsObj.getJSONObject("content");
								 								 								 
								 JSONArray messages = content.getJSONArray("message");
								 
								 JSONArray gifts = content.getJSONArray("gift");
								 
								 JSONArray advertise = content.getJSONArray("advertise");
								 
								 try{
								 JSONArray giftsDaily = content.getJSONArray("daily_gift");										
								 for(int i=0; i<giftsDaily.length(); i++){
										 JSONObject ele = giftsDaily.getJSONObject(i);
										 ctr.d.dailyGifts.add(new DailyGift(ele.getInt("zegome_gold"), ele.getString("message")));
									 }
								 }
								 catch(Exception ex){}
								 
								 for(int i=0; i<messages.length(); i++){
									 JSONObject ele = messages.getJSONObject(i);
									 ctr.autoRunText.addNotify(0,ctr.d.isVietnamese?ele.getString("message_content"):ele.getString("message_eng"),null,null);
								 }
								 
								 for(int i=0; i<gifts.length(); i++){
									 JSONObject ele = gifts.getJSONObject(i);
									 //ctr.a.notify.add(ele.getString("gift_id") +"-"+ele.getString("user_send")+"-"+ele.getString("zegome_gold")+"-"+ele.getString("coupon")+"-"+ele.getString("message"));
									 ctr.d.gifts.add(new Gift(ele.getInt("gift_id"), ele.getString("user_send"), ele.getInt("zegome_gold"), ele.getString("coupon"), ele.getString("message")));
								 }
								 
								 for(int i=0; i<advertise.length(); i++){
									 JSONObject ele = advertise.getJSONObject(i);
									 //ctr.a.notify.add(ele.getString("gift_id") +"-"+ele.getString("user_send")+"-"+ele.getString("zegome_gold")+"-"+ele.getString("coupon")+"-"+ele.getString("message"));									 
									 ctr.d.gameAdvertiseLoad.add(new GameAdvertise(i,ele.getString("icon"), ele.getString("link"),ctr.d.isVietnamese?ele.getString("message_vn"):ele.getString("message_eng")));
								 }
								 								
								 								 
								 ctr.d.moneyOnline = content.getInt("user_gome");
								 ctr.d.level = content.getInt("level");
								 ctr.d.total_win = content.getInt("total_win");
								 ctr.d.total_lose = content.getInt("total_lose");
								 ctr.d.achievement = content.getString("achievement");
								 ctr.d.exp = content.getInt("experiment");
								 ctr.d.star = content.getInt("star");
								 ctr.d.saveAccount();													 								 
								 ctr.stageHelper.dimissDialogLoading();
								 ctr.d.isLogined = true;
								 ctr.d.isHasAccount = true;
								 ctr.d.isSendingLogin = false;	
							 }
							 else {
								 logout();
								 //ctr.stageHelper.createDialogErrorLogin(stage, tweenManager, ctr.lang.error+": "+jsObj.getString("content"), 2, null);
								 ctr.d.isSendingLogin = false;
							 }
							 
						}
					}
						catch(Exception ex){
							ctr.androidHelper.log(ex.getMessage());
						}
					}
				};				
				ctr.d.namePre = ctr.d.name;
				ctr.d.passPre = ctr.d.pass;				
				ctr.androidHelper.login(loginSuccess);				
			}
		}
		}
		
		if(ctr.d.isSendingAvatar == false){
			if(ctr.d.isNetworkAvaiable && ctr.d.isHasAccount){
				if(ctr.d.isLogined==true && ctr.d.isCheckAvatar == false){					
					ctr.d.isSendingAvatar = true;
					ctr.androidHelper.sendCheckAvatar(this);					
				}
			}
		}
		
		if(ctr.d.isNewAvatarNotSync == true && ctr.d.isSyncedNewAvatar == false && ctr.d.isCheckAvatar == true){			
			if(ctr.d.isSyncingNewAvatar == false){
				ctr.d.isSyncingNewAvatar = true;
				if(ctr.d.isHasAccount && ctr.d.isLogined && ctr.d.isNetworkAvaiable){	
		            byte[] bytes = null;
		            String send = null;
		            File file = new File("/sdcard/data/avatar/"+ctr.d.name);
		            		   InputStream in = null;
		            		   try {
		            			   in = new BufferedInputStream(
		            					   new FileInputStream(file));
		            			   bytes = ctr.ioHelper.readBytes(in);
		            			   send = Base64.encodeToString(bytes, 0);
		            		   }
		            		   catch(Exception ex){}
		            		   finally{
		            			   if (in != null) {
			            		    	 try{
			            		       in.close();
			            		    	 }
			            		    	 catch(Exception ex){
			            		    		 ctr.androidHelper.log(ex.getMessage());
			            		    	 }
			            		     }
		            		   }
				ctr.androidHelper.sendNewAvatar(send);
								
				}
			}
		}
	}
		for (int i=0;i<ctr.d.backLis.size();i++) {
			BackListener e = ctr.d.backLis.elementAt(i);
			if(e.isRemoveAble)
				ctr.d.backLis.removeElementAt(i);
		}
	}
	
	public void clear(){
		iconGomeSmall = null;
		if(iconGomeSmallStage!=null){
			iconGomeSmallStage.remove();
			iconGomeSmallStage = null;			
		}
		stage.clear();
		tweenManager.killAll();
		//stage = null;
		//tweenManager = null;
					
		black = null;	
				
		load = null; loaded = null; end = null;
	}
	
	
	public Stage getStage(){
		return stage;		
	}
	
	public TweenManager getTween(){
		return tweenManager;		
	}	

	public void backKeyProcess(){
		ctr.stageHelper.createDialog(stage, tweenManager, "Dialog Back Press", 1, null, null);
	}
		
	public void gotoScreen(IMAction load, IMAction loaded, IMAction end)
	{		
		load.action();
		state = 2;
		this.loaded = loaded;
		this.end = end;
	}

	@Override
	public boolean keyDown(int arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyTyped(char arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyUp(int keycode) {
		// TODO Auto-generated method stub
		if(keycode == Keys.BACK){	      	
			if(ctr.a.isHasDialog() == false){
				backKeyProcess();
			}
			else{
				if(ctr.a.isHasDialog()){
					if(ctr.d.backLis.size()>0)
						//postRunnable(new Runnable() {
							
						//	@Override
						//	public void run() {
								// TODO Auto-generated method stub
								if(!ctr.d.backLis.isEmpty())
								ctr.d.backLis.elementAt(ctr.d.backLis.size()-1).checkBack();
						//	}
						//});
				}
			}
			return true;
	    }
		return false;
	}

	@Override
	public boolean mouseMoved(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean scrolled(int arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDown(int arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDragged(int arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onComplete(String arg0) {
		// TODO Auto-generated method stub
		//Log.e("FFFFFFFFFFFFFFFFFFFFFFFFFF", "day mà1" + " "+ mAction);
		switch (mAction) {
		case IFacebookProcessorListener.LOGIN:
			ctr.d.isLoginedFB = true;						
			if(afterAction != null){				
				ctr.a.isFbAction = false;
				ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.loginFBSucess, 1, new ChangeListener() {
					
					@Override
					public void changed(ChangeEvent arg0, Actor arg1) {
						// TODO Auto-generated method stub
						afterAction.action();		
					}
				}, null);									
			}
			else{
				ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.loginFBSucess, 1, null, null);			
				ctr.a.isFbAction = false;
			}
			break;
		case IFacebookProcessorListener.SEND_REQUEST:		
			ctr.a.isFbAction = false;
			if(ctr.d.passTask[D.TASK_FB_SHARE] == false){
				ctr.d.passTask[D.TASK_FB_SHARE] = true;
				ctr.d.saveAccount();
			}
			ctr.d.money+=500;
			ctr.d.saveAccount();
			ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.bonusDiamond , 1, new ChangeListener() {				
				@Override
				public void changed(ChangeEvent arg0, Actor arg1) {
					// TODO Auto-generated method stub
					//ctr.androidHelper.sendBonusExp(stage, tweenManager);
				}
			}, null);
			break;
		case IFacebookProcessorListener.FEED:		
			ctr.a.isFbAction = false;
			if(ctr.d.passTask[D.TASK_FB_FEED] == false){
				ctr.d.passTask[D.TASK_FB_FEED] = true;
				ctr.d.saveAccount();
			}
			ctr.d.money+=500;
			ctr.d.saveAccount();
			ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.bonusDiamond , 1, new ChangeListener() {				
				@Override
				public void changed(ChangeEvent arg0, Actor arg1) {
					// TODO Auto-generated method stub
					//ctr.androidHelper.sendBonusExp(stage, tweenManager);
				}
			}, null);
			break;
		case IFacebookProcessorListener.PUBLISH_STORY:	
			ctr.a.isFbAction = false;
			if(ctr.d.passTask[D.TASK_FB_STORY] == false){
				ctr.d.passTask[D.TASK_FB_STORY] = true;
				ctr.d.saveAccount();
			}
			ctr.d.money+=500;
			ctr.d.saveAccount();
			ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.bonusDiamond , 1, new ChangeListener() {				
				@Override
				public void changed(ChangeEvent arg0, Actor arg1) {
					// TODO Auto-generated method stub
					//ctr.androidHelper.sendBonusExp(stage, tweenManager);
				}
			}, null);
			break;
		}
	}

	@Override
	public void onFailure(String arg0) {
		// TODO Auto-generated method stub
		//Log.e("FFFFFFFFFFFFFFFFFFFFFFFFFF", "day mà2" + " "+ mAction);
		switch (mAction) {
		case IFacebookProcessorListener.LOGIN:			
			//ctr.stageHelper.createDialog(stage, tweenManager, "Đăng nhập Facebook thất bại! Vui lòng thử lại", 1, null, null);			
			ctr.a.isFbAction = false;
			break;
		case IFacebookProcessorListener.SEND_REQUEST:	
			ctr.a.isFbAction = false;
			break;
		case IFacebookProcessorListener.FEED:			
			ctr.a.isFbAction = false;
			break;
		case IFacebookProcessorListener.PUBLISH_STORY:			
			ctr.a.isFbAction = false;
			break;
		}
	}
	
	public void logout(){
		ctr.d.isHasAccount=false;

		
		ctr.d.isLogined = false;	

										
		ctr.d.isSendingGetSessionId = false;
		ctr.d.isHasSessionId = false;
											
		ctr.d.isSendingAvatar = false;
		ctr.d.isCheckAvatar = false;
		
		ctr.d.timeAvatar = "2012-09-24 00:00:01";
	    ctr.d.isNewAvatarNotSync = false;
		ctr.d.isSyncingNewAvatar = false;
		ctr.d.isSyncedNewAvatar = false;
		ctr.d.saveAvatarInfo();
		
		ctr.d.moneyOnline = 0;
		ctr.d.name = "";	
		ctr.d.email = "";
		ctr.d.pass = "";										
		
		ctr.d.namePre = "";	
		ctr.d.passPre = "";		
																				
		ctr.d.saveAccount();
		
		postRunnable(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					TextureAtlas cm2Atlas = ctr.controller.get(ctr.g.ASSET_FOLDER_DATA+"commond/commond2.atlas", TextureAtlas.class);
					ctr.a.avatarMe = cm2Atlas.findRegion("avatar");	
					ctr.a.avatarMe.setRegionWidth(64);
					ctr.a.avatarMe.setRegionHeight(64);
					ctr.stageHelper.avatar.setDrawable(new TextureRegionDrawable(ctr.a.avatarMe));
				}
			});
	}
}
